package com.sinosun.autotestmachine.tools.parser;

import java.util.ArrayList;
import java.util.List;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.sinosun.autotestmachine.tools.proxy.ErrorCode;

import android.util.Log;


public class NetInformationParser {
	JSONObject jo = null;

	public NetInformationParser(String jsonStr) {
		try {
			jo = new JSONObject(jsonStr);
		} catch (JSONException e) {
			e.printStackTrace();
			jo = null;
		}
	}
	
	public int getType() {
		if (jo.has(ParamKey.TYPE)) {
			try {
				return jo.getInt(ParamKey.TYPE);
			} catch (JSONException e) {
				e.printStackTrace();
				return ErrorCode.ERR_PARAM_ERROR;
			}
		}
		return ErrorCode.ERR_PARAM_ERROR;
	}
	
	public int getCode() {
		if (jo.has(ParamKey.CODE)) {
			try {
				return jo.getInt(ParamKey.CODE);
			} catch (JSONException e) {
				e.printStackTrace();
				return ErrorCode.ERR_PARAM_ERROR;
			}
		}
		return ErrorCode.ERR_PARAM_ERROR;
	}
	
	public String getTaskId() {
		if (jo.has(ParamKey.TASK_ID)) {
			try {
				return jo.getString(ParamKey.TASK_ID);
			} catch (JSONException e) {
				e.printStackTrace();
				return null;
			}
		}
		return null;
	}
	
	public String getTask() {
		if (jo.has(ParamKey.TASK_ID_DEL)) {
			try {
				if (jo.isNull(ParamKey.TASK_ID_DEL)) {
					return null;
				}
				return jo.getString(ParamKey.TASK_ID_DEL);
			} catch (JSONException e) {
				e.printStackTrace();
				return null;
			}
		}
		return null;
	}
	
	public List<String> getContent() {
		List<String> ret = new ArrayList<String>();
		if (jo.has(ParamKey.CONTENT)) {
			try {
				JSONArray ja = jo.getJSONArray(ParamKey.CONTENT);
				Log.e("XXX", "length -> " + ja.length());
				for (int i = 0; i < ja.length(); i++) {
					ret.add(ja.getString(i));
					Log.e("XXX", "ja -> i -> " + i + "" + ja.getString(i));
				}
			} catch (JSONException e) {
				e.printStackTrace();
				ret = new ArrayList<String>();
			}
		}
		return ret;
	}
}
